<template>
  <div class="process" ref="process">
    <span class="endtime">{{endtime}}</span>
    <div class="process" :style="{width}"></div>
    <div class="thunk" ref="trunk" :style="{left}">
      <div class="block"></div>
      <div class="tips">
        <span>{{timeshow}}</span>
        <i class="fas fa-caret-down"></i>
      </div>
    </div>
  </div>
</template>
<script>
/*
 * min 进度条最小值
 * max 进度条最大值
 * v-model 对当前值进行双向绑定实时显示拖拽进度
 * */
import {
  getTsFormatDate,
  ChangeHourMinutestr
} from "../../assets/js/common.js";
export default {
  props: ["min", "max", "value", "start"],
  data() {
    return {
      process: null, //滚动条DOM元素
      thunk: null, //拖拽DOM元素
      per: this.value, //当前值
      endtime: "24:00",
      starttime: this.start
    };
  },
  //渲染到页面的时候
  mounted() {
    this.process = this.$refs.process;
    this.thunk = this.$refs.trunk;
    var _this = this;
    this.thunk.onmousedown = function(e) {
      var width = parseInt(_this.width);
      var disX = e.clientX;
      document.onmousemove = function(e) {
        // value, left, width
        // 当value变化的时候，会通过计算属性修改left，width

        // 拖拽的时候获取的新width
        var newWidth = e.clientX - disX + width;
        // 拖拽的时候得到新的百分比
        var scale = newWidth / _this.process.offsetWidth;
        _this.per = Math.ceil((_this.max - _this.min) * scale + _this.min);
        _this.per = Math.max(_this.per, _this.min);
        _this.per = Math.min(_this.per, _this.max);
      };
      document.onmouseup = function() {
        document.onmousemove = document.onmouseup = null;
      };
      return false;
    };
  },
  watch: {
    start(newvalue, oldvalue) {
      console.log(newvalue + " " + oldvalue);
      this.starttime = newvalue;
    },
    timeshow(newvalue){
      this.$emit("handleprolong",newvalue)
    }
  },
  computed: {
    // 设置一个百分比，提供计算slider进度宽度和trunk的left值
    // 对应公式为  当前值-最小值/最大值-最小值 = slider进度width / slider总width
    // trunk left =  slider进度width + trunk宽度/2
    scale() {
      return (this.per - this.min) / (this.max - this.min);
    },
    timeshow() {
      var totalmin = 0;
      var starthour = parseInt(this.starttime.slice(0, 2)) * 60;
      var startmin = parseInt(this.starttime.slice(3));
      //TODO
      var min = Math.round(
        (1440 - (starthour + startmin)) * this.scale + (starthour + startmin)
      );
      return ChangeHourMinutestr(min);
    },
    width() {
      if (this.process) {
        return this.process.offsetWidth * this.scale + "px";
      } else {
        return 0 + "px";
      }
    },
    left() {
      if (this.process) {
        return (
          this.process.offsetWidth * this.scale -
          this.thunk.offsetWidth / 2 +
          "px"
        );
      } else {
        return 0 + "px";
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.box {
  margin: 100px auto 0;
  width: 80%;
}
.clear:after {
  content: "";
  display: block;
  clear: both;
}
.process {
  position: relative;
  margin: 20px 0;
  width: 90%;
  height: 10px;
  background: #e4e7ed;
  border-radius: 5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  .endtime {
    position: absolute;
    right: -50px;
  }
}
.process .process {
  position: absolute;
  left: 0;
  top: -20px;
  width: 112px;
  height: 10px;
  border-radius: 5px;
  background: #409eff;
}
.process .thunk {
  position: absolute;
  left: 100px;
  top: -7px;
  width: 20px;
  height: 20px;
}
.process .block {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid #409eff;
  background: rgba(255, 255, 255, 1);
  transition: 0.2s all;
}
.process .tips {
  position: absolute;
  left: -7px;
  bottom: 30px;
  min-width: 15px;
  text-align: center;
  padding: 4px 8px;
  background: #000;
  border-radius: 5px;
  height: 24px;
  color: #fff;
}
.process .tips i {
  position: absolute;
  margin-left: -5px;
  left: 50%;
  bottom: -9px;
  font-size: 16px;
  color: #000;
}
.process .block:hover {
  transform: scale(1.1);
  opacity: 0.6;
}
</style>